package com.skyline.wizard.indexer.impl;

import java.io.File;
import java.io.IOException;
import java.util.List;

import net.paoding.analysis.analyzer.PaodingAnalyzer;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import com.skyline.wizard.indexer.Indexer;

/**
 * Class <code>IndexerImpl</code> 对Indexer的实现
 * 
 * @author Jairus Chan
 * @version 0.1, 11/30/10
 * @since JDK1.6
 */

public class BaseIndexerImpl implements Indexer {

	private int maxBufferedDocs = 500;
	private int mergeFactor = 500;
	private Analyzer analyzer;
	private String indexPath;

	protected void inital(String indexPath) {
		if (analyzer == null) {
			analyzer = new PaodingAnalyzer();
		}
		this.indexPath = indexPath;
	}

	public void createIndex() {
		try {
			File file= new File(indexPath);
			if(file.exists()==false){
				file.mkdirs();
			}
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexWriter indexWriter = new IndexWriter(directory, analyzer, true, MaxFieldLength.LIMITED);
			indexWriter.commit();
			indexWriter.close(true);
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addDocument(Document d) {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexWriter indexWriter = new IndexWriter(directory, analyzer, false, MaxFieldLength.LIMITED);
			indexWriter.setMaxBufferedDocs(maxBufferedDocs);
			indexWriter.setMergeFactor(mergeFactor);
			indexWriter.addDocument(d);
			indexWriter.commit();
			indexWriter.close(true);
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void addDocuments(List<Document> docs) {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexWriter indexWriter = new IndexWriter(directory, analyzer, false, MaxFieldLength.LIMITED);
			indexWriter.setMaxBufferedDocs(maxBufferedDocs);
			indexWriter.setMergeFactor(mergeFactor);
			for (Document document : docs) {
				indexWriter.addDocument(document);
			}
			indexWriter.commit();
			indexWriter.close(true);
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	public void deleteDocument(int docID) {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexReader indexReader = IndexReader.open(directory);
			indexReader.deleteDocument(docID);
			indexReader.close();
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("deprecation")
	public void deleteDocuments(String key, String value) {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			Term term = new Term(key, value);
			IndexReader indexReader = IndexReader.open(directory);
			indexReader.deleteDocuments(term);
			indexReader.close();
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void modifyDocument(Document d) {
		deleteDocuments("id", d.get("id"));
		addDocument(d);
	}

	public void modifyDocuments(List<Document> docs) {
		deleteDocuments(docs);
		addDocuments(docs);
	}

	@SuppressWarnings("deprecation")
	public void deleteDocuments(List<Document> docs) {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexReader indexReader = IndexReader.open(directory);
			for (Document document : docs) {
				Term term = new Term("id", document.get("id"));
				indexReader.deleteDocuments(term);
			}
			indexReader.close();
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void optimize() {
		try {
			Directory directory = FSDirectory.open(new File(indexPath));
			IndexWriter indexWriter = new IndexWriter(directory, analyzer, false, MaxFieldLength.LIMITED);
			indexWriter.optimize();
			indexWriter.close(true);
			directory.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
